package com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bean.FreeReadListBean;
import com.myapp.MyApp;
import com.util.GlideUtils;
import com.xiaochun.hxhj.R;

import java.util.List;

/**
 * Created by fei on 2017/12/30.
 */

public class FreeReadRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<FreeReadListBean.DataBeanX.DataBean> mDatas;
    private Context context;
    private MyApp myApp;

    public FreeReadRecyclerViewAdapter(List<FreeReadListBean.DataBeanX.DataBean> mDatas, Context context, MyApp myApp) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }


    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<FreeReadListBean.DataBeanX.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
        /*if (datas.size() > 15){
            notifyItemRangeChanged(datas.size()-15,15);
        }else{
            notifyItemRangeChanged(0,datas.size()-1);
        }*/
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_freeread, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
            final FreeReadListBean.DataBeanX.DataBean bean = mDatas.get(pos);
            if (viewHolder instanceof Holder) {

            GlideUtils.loadRoundCircleImage(context, bean.getImage(), ((Holder) viewHolder).img);
//                ((Holder) viewHolder).img.setImageResource(R.mipmap.ic_launcher);


            ((Holder) viewHolder).tv_name.setText(bean.getName());
            ((Holder) viewHolder).tv_authorone.setText(bean.getAuthor());
                int free_switch = bean.getFree_switch();
                if (free_switch == 0){
                    //付费\
                    ((Holder) viewHolder).tv_freeone_flag.setText("收费");
                    ((Holder) viewHolder).tv_freeone_flag.setVisibility(View.VISIBLE);
                    ((Holder) viewHolder).tv_freeone_flag.setBackgroundResource(R.drawable.buy_flag_bg);
                }else if (free_switch==1){
                    //免费
                    ((Holder) viewHolder).tv_freeone_flag.setText("免费");
                    ((Holder) viewHolder).tv_freeone_flag.setVisibility(View.VISIBLE);
                    ((Holder) viewHolder).tv_freeone_flag.setBackgroundResource(R.drawable.free_flag_bg);
                }else{
                    ((Holder) viewHolder).tv_freeone_flag.setVisibility(View.GONE);
                }

                if (mListener != null) {
                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.onItemClick(pos);
                        }
                    });
                }
            }

    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private ImageView img;//图片
        private TextView tv_name;//名字
        private TextView tv_content;//介绍
        private TextView tv_authorone;//查看详情
        private TextView tv_freeone_flag;//查看详情

        public Holder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img_freespeedone);
            tv_name = (TextView) itemView.findViewById(R.id.tv_freespeedone);
            tv_authorone = (TextView) itemView.findViewById(R.id.tv_authorone);
            tv_freeone_flag = (TextView) itemView.findViewById(R.id.tv_freeone_flag);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}